﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MISAPP.MISInterface;
using MISAPP.Classify.Service;

public partial class View_MIS_ARTM95_ABCDE : ViewMatrix //System.Web.UI.Page
{
    Service_ARTM95_ABCDE.AccNoOption option;
    string errMsg = string.Empty;
    bool isWMG = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        errMsg = string.Empty;
        
        Title = "會計科目餘額查詢";
        criteriaList = new string[] { "Criteria", "AccBetween", "Year", "Option" };
        serviceName = "MISAPP.Classify.Service.Service_ARTM95_ABCDE";
        if (!IsPostBack)
        {
            FillCriteria("GetCriteriaData", "SetData");
            txtYear.Text = DateTime.Now.Year.ToString();
        }
        ucAccNOS.SetLabelClick();
        ucAccNOE.SetLabelClick();

    }

    protected void execute()
    {
        List<iSelectList> CriteriaList = GetSelectCLB();
        //List<string> dateBetween = datePickerFromTo1.GetBetweenDate();
        List<string> accBetween = new List<string>
        { 
            ucAccNOS.GetAutoCompleteValue()
            , ucAccNOE.GetAutoCompleteValue() 
        };
        Session["Criteria"] = CriteriaList;
        Session["AccBetween"] = accBetween;
        Session["Year"] = txtYear.Text.Length==0 ? 
            DateTime.Now.Year.ToString():txtYear.Text ;
        Session["Option"] =  (int)option;
        Session["ShowTotal"] = chk1.Checked;
    }

    protected void btn_ExecuteClick(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        isWMG = btn.Text.Equals(btnWMG.Text) ? true : false;
        if (validate(btn.Text.Substring(0, 1)).Equals(false))
        {
            lblResult.Text = errMsg;
            return;
        }        

        execute();
        Server.Transfer("ARTM95_ABCDE_1.aspx");
    }

    protected bool validate(string p_opt)
    {
        bool result = false;
        //開始、結束科目不得為空白
        if (!isWMG)
        {
            if ((ucAccNOS.GetAutoCompleteValue().Length == 0) &
                (ucAccNOE.GetAutoCompleteValue().Length == 0))
            {
                errMsg = "開始、結束科目不得為空白！";
                return result;
            }        
        }

        switch (p_opt)
        {
            case "1":
                option = Service_ARTM95_ABCDE.AccNoOption.Accno123;
                result = validateWtihAccno("AccountNoIs123");
                errMsg = result.Equals(false) ? "請輸入開頭為科目1、2、3" 
                    : "";
                break;
            case "4":
                option = Service_ARTM95_ABCDE.AccNoOption.Accno45;
                result = validateWtihAccno("AccountNoIs45");
                errMsg = result.Equals(false) ? "請輸入開頭為科目4、5"
                    : "";
                break;
            case "W":
                option = Service_ARTM95_ABCDE.AccNoOption.AccWMG;
                result = true;
                break;
            default:
                option = Service_ARTM95_ABCDE.AccNoOption.AccnoDefault;
                result = true;
                break;
        }
        return result;
    }

    protected bool validateWtihAccno(string p_method)
    {
        Type[] paraType = new Type[] { typeof(System.String) };
        System.Reflection.MethodInfo mi = GetObjectMethodInfo(p_method);
        bool result = false;

        if ((ucAccNOS.GetAutoCompleteValue().Length > 0) &
            (ucAccNOE.GetAutoCompleteValue().Length > 0))
        {

            result = DoMethodInvokeWithBool(mi
                , new object[] { ucAccNOS.GetAutoCompleteValue() });
            if (result.Equals(true))
            {
                result = DoMethodInvokeWithBool(mi
                    , new object[] { ucAccNOE.GetAutoCompleteValue() });
            }
        }
        else
        {
            string tmp = string.Format("{0}{1}", ucAccNOS.GetAutoCompleteValue()
                , ucAccNOE.GetAutoCompleteValue());
            result = DoMethodInvokeWithBool(mi, new object[] { tmp });
        }
        return result;
    }

    protected bool DoMethodInvokeWithBool(System.Reflection.MethodInfo mi
        , object[] p_objs)
    {
        object boolObj = DoMethodInvoke(mi, Instance, p_objs);
        return Convert.ToBoolean(boolObj);
    }
}
